/**
 * 演示程序当前的 “注册/登录” 等操作，是基于 “本地存储” 完成的
 * 当您要参考这个演示程序进行相关 app 的开发时，
 * 请注意将相关方法调整成 “基于服务端Service” 的实现。
 **/
(function($, owner) {
	/**
	 * 用户登录
	 **/
	owner.login = function(loginInfo, callback) {
		callback = callback || $.noop;
		loginInfo = loginInfo || {};
		loginInfo.account = loginInfo.account || '';
		loginInfo.password = loginInfo.password || '';
		if(loginInfo.account.length < 11) {
			return callback('账号最短为 11 个字符');
		}
		if(!(/^1(3|4|5|7|8)\d{9}$/.test(loginInfo.account))) {
			return callback("手机号码有误，请重填");
		}
		if(loginInfo.password.length < 6) {
			return callback('密码最短为 6 个字符');
		}
		var user = JSON.parse(localStorage.getItem('userinfo') || '[]');
		loginInfo.passwd = md5(loginInfo.password);
		/*
		var authed = users.some(function(user) {
			return loginInfo.account == user.account && loginInfo.password == user.password;
		});*/
		if(loginInfo.account == user.account && md5(loginInfo.passwd) == user.passwd) {
			return owner.createState(loginInfo.account, callback);
		} else {
			//console.log(loginurl);
			
			mui.ajax(loginurl, {
			data: loginInfo,
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			headers: {
				'Content-Type': 'application/json'
			},
			success: function(data) {
				console.log(data.msg);
				//服务器返回响应，根据响应结果，分析是否登录成功；
				//...
				return callback();
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				console.log(type);
			}
		});
			//return callback('用户名或密码错误');
		}
	};

	owner.createState = function(name, callback) {
		var state = owner.getState();
		state.account = name;
		state.token = "token123456789";
		owner.setState(state);
		return callback();
	};

	/**
	 * 新用户注册
	 **/
	owner.reg = function(regInfo, callback) {
		callback = callback || $.noop;
		regInfo = regInfo || {};
		regInfo.account = regInfo.account || '';
		regInfo.password = regInfo.password || '';
		if(regInfo.account.length < 11) {
			return callback('手机号最短需要 11 个字符');
		}
		if(!(/^1(3|4|5|7|8)\d{9}$/.test(regInfo.account))) {
			return callback("手机号码有误，请重填");
		}
		if(regInfo.password.length < 6) {
			return callback('密码最短需要 6 个字符');
		}
		regInfo.passwd = md5(regInfo.password);
		console.log(regInfo.account);
		console.log(regInfo.passwd);
		console.log(regurl); 
		
		mui.ajax(regurl, {
			data: regInfo,
			dataType: 'json', //服务器返回json格式数据
			type: 'get', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			
			success: function(data) {
				console.log(data);
				//服务器返回响应，根据响应结果，分析是否登录成功；
				//... 
				if(data.status > 0) {
					//var users = JSON.parse(localStorage.getItem('userinfo') || '[]');
					//users.push(regInfo);
					localStorage.setItem('userinfo', JSON.stringify(data.info));
					return callback();
				}else {
					return callback(data.msg);
				}
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				console.log(type);
				console.log(xhr);
				console.log(errorThrown);
			}
		});
		/*
		mui.post(regurl, regInfo, function(res) {
			console.log(res);
			console.log(JSON.stringify(res));
		}, 'jsonp');*/

		
	};

	/**
	 * 获取当前状态
	 **/
	owner.getState = function() {
		var stateText = localStorage.getItem('$state') || "{}";
		return JSON.parse(stateText);
	};

	/**
	 * 设置当前状态
	 **/
	owner.setState = function(state) {
		state = state || {};
		localStorage.setItem('$state', JSON.stringify(state));
		//var settings = owner.getSettings();
		//settings.gestures = '';
		//owner.setSettings(settings);
	};

	var checkEmail = function(email) {
		email = email || '';
		return(email.length > 3 && email.indexOf('@') > -1);
	};

	/**
	 * 找回密码
	 **/
	owner.forgetPassword = function(email, callback) {
		callback = callback || $.noop;
		if(!checkEmail(email)) {
			return callback('邮箱地址不合法');
		}
		return callback(null, '新的随机密码已经发送到您的邮箱，请查收邮件。');
	};

	/**
	 * 获取应用本地配置
	 **/
	owner.setSettings = function(settings) {
		settings = settings || {};
		localStorage.setItem('$settings', JSON.stringify(settings));
	}

	/**
	 * 设置应用本地配置
	 **/
	owner.getSettings = function() {
		var settingsText = localStorage.getItem('$settings') || "{}";
		return JSON.parse(settingsText);
	}
	/**
	 * 获取本地是否安装客户端
	 **/
	owner.isInstalled = function(id) {
		if(id === 'qihoo' && mui.os.plus) {
			return true;
		}
		if(mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var packageManager = main.getPackageManager();
			var PackageManager = plus.android.importClass(packageManager)
			var packageName = {
				"qq": "com.tencent.mobileqq",
				"weixin": "com.tencent.mm",
				"sinaweibo": "com.sina.weibo"
			}
			try {
				return packageManager.getPackageInfo(packageName[id], PackageManager.GET_ACTIVITIES);
			} catch(e) {}
		} else {
			switch(id) {
				case "qq":
					var TencentOAuth = plus.ios.import("TencentOAuth");
					return TencentOAuth.iphoneQQInstalled();
				case "weixin":
					var WXApi = plus.ios.import("WXApi");
					return WXApi.isWXAppInstalled()
				case "sinaweibo":
					var SinaAPI = plus.ios.import("WeiboSDK");
					return SinaAPI.isWeiboAppInstalled()
				default:
					break;
			}
		}
	}
}(mui, window.app = {}));